package com.bailiny.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author Yang Xu
 * @date 2020/1/15 9:10
 * @description: mapper类继承Mapper类表示为一个标准的Mapper类，需定义4个泛型
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text text = new Text();
    private IntWritable intWritable = new IntWritable();
    /**
     * 覆写map方法，每一行数据都会调用map方法，我们的处理逻辑都写在这个map方法里
     * @param key key1，行偏移量，一般没啥用，可以
     * @param value value1，行文本内容，需要切割，转换为新的k2、v2输出
     * @param context 上下文对象，承接上文，把数据传输给下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split(",");
        for (String word: split) {
            // 写出k2,v2，这里的类型与k2,v2一致
            text.set(word);
            intWritable.set(1);
            context.write(text, intWritable);
        }


    }
}
